package com.dingaolin.designpattern.chainofresponsibility.mypractice;

import java.util.ArrayList;
import java.util.List;

public class FilterChain implements Filter {
    public List<Filter> filterList = new ArrayList<>();
    int index = 0;

    public FilterChain addFilter(Filter filter) {
        filterList.add(filter);
        return this;
    }

    @Override
    public void doFilter(Message request, Message response, FilterChain filterChain) {
        if (index == filterList.size()) {
            return;
        }

        Filter filter = filterList.get(index);
        index++;
        filter.doFilter(request, response, filterChain);
    }
}
